import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jxchmz/%E4%BB%93%E7%AE%A1/%E5%BA%93%E5%AD%98%E8%B0%83%E6%8B%A8/GoodsTransfer.dart';
import 'package:jxchmz/API/%E5%BA%93%E5%AD%98%E7%AE%A1%E7%90%86/KuCunManager.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';

class GoodsTransferList extends StatefulWidget {
  @override
  _GoodsTransferListState createState() => new _GoodsTransferListState();
}

class _GoodsTransferListState extends State<GoodsTransferList> {
  var OrderList = [];
  // 当前页数
  int _page = 1;
  // 是否还有
  bool _hasMore = true;
  // 滚动控制器
  ScrollController _scrollController = new ScrollController();
//页面加载完成时
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 监听滚动事件
    _scrollController.addListener(() {
      var maxScroll = _scrollController.position.maxScrollExtent;
      var pixel = _scrollController.position.pixels;
      if (maxScroll == pixel) {
        if (_hasMore) {
          _page++;
          GetTransferOrderList();
        }
      }
    });
    Future.delayed(Duration.zero,(){
      GetTransferOrderList();
    });
  }

  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当 State 被暂时从视图树中移除时，会调用这个方法，同时页面切换时，也会调用。
  @override
  void deactivate() {
    // TODO: implement dispose
    super.deactivate();
  }

  ///在 initState() 之后调用，当 State 对象的依赖关系发生变化时，该方法被调用，初始化时也会调用。
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  // TODO: add state variables and methods
  @override
  Widget build(BuildContext context) {
    // TODO: add widget build method
    return  Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text("调拨单列表", style: TextStyle(fontSize: 15, color: Colors.black),),
          centerTitle: true,
          elevation: 0,
          iconTheme: IconThemeData(
              color: Colors.black
          ),
        ),
        body: Container(
          padding: EdgeInsets.all(15),
          child: ListView.separated(
            controller: _scrollController,
            itemCount: OrderList.length,
            itemBuilder: (context,index){
              return  GestureDetector(
                behavior: HitTestBehavior.opaque,
                child: Container(
                  margin: EdgeInsets.only(top: 15),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("Documentnumber".tr+":${OrderList[index]['billNo']}"),
                            alignment: Alignment.centerLeft,
                          ),
                          // Container(
                          //   child: Text("${OrderList[index]['billStage']==0? "Notcounted".tr : OrderList[index]['billStage']==1?"Endofinventory".tr:"Inventory".tr}",
                          //     style: TextStyle(
                          //         color:OrderList[index]['billStage']==0? Colors.red : OrderList[index]['billStage']==1?Colors.green:Colors.orange),),
                          //   alignment: Alignment.centerLeft,
                          // ),
                        ],

                      ),
                      Container(
                        height: 5,
                      ),
                      Container(
                        child: Row(
                          children: [
                            Text("Currentwarehouse".tr+"：${OrderList[index]['jxcWarehouseIdAName'].toString()}"),
                            Text("Targetwarehouse".tr+"：${OrderList[index]['jxcWarehouseIdBName'].toString()}"),
                          ],
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        ),
                      ),
                      Container(
                        height: 5,
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        child: Text("Transferquantity".tr+":${OrderList[index]['InfoList'].length}"),
                      )
                    ],
                  ),
                ),
                onTap: () {
                  //前往盘点详情
                  Get.to(GoodsTransfer(),arguments: {"Order":OrderList[index]})!.then((value){
                    //返回刷新盘点单
                    print("刷新调拨单");
                    _page = 1;
                    _hasMore = true;
                    OrderList = [];
                    GetTransferOrderList();
                  });
                },

              );
            },
            separatorBuilder: (context,index){
              return Divider();
            },
          ),
        )
    );
  }
  GetTransferOrderList() async {
    //搜索参数
    var data = {
      "pageNum": _page,
      "pageSize": 10,
      "reasonable": false,
    };
    var ResponseData = await KuCunManager().GetTransferOrderList(data);
    if (ResponseData['code'] == 200) {
      var List = ResponseData['rows'];
      if (List.length <= 0) {
        // 关闭加载
        this._hasMore = false;
      } else {
        List.forEach((element) {
          OrderList.add(element);
        });
      }
    } else {
      new GetxTools().ErrorToast("Failedtoobtaininventoryorderinformation".tr);
    }
    setState(() {});
  }
}
